package com.xuecheng.content.service.impl;

import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.model.dto.CourseCategoryTreeDto;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.service.CourseCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author VectorX
 * @version V1.0
 * @description
 * @date 2024-04-08 16:35:10
 */
@Slf4j
@Service
public class CourseCategoryServiceImpl implements CourseCategoryService
{

    @Autowired
    private CourseCategoryMapper courseCategoryMapper;

    @Override
    public List<CourseCategoryTreeDto> queryTreeNodes(String id) {
        List<CourseCategoryTreeDto> courseCategoryTreeDtoList = courseCategoryMapper.selectTreeNodes(id);

        // 将list转map,以备使用,排除根节点
        Map<String, CourseCategoryTreeDto> mapTemp = courseCategoryTreeDtoList
                .stream()
                .filter(item -> !id.equals(item.getId()))
                .collect(Collectors.toMap(CourseCategory::getId, Function.identity(), (key1, key2) -> key2));

        // 最终返回的list
        List<CourseCategoryTreeDto> categoryTreeDtoList = new ArrayList<>();
        // 依次遍历每个元素,排除根节点
        courseCategoryTreeDtoList
                .stream()
                .filter(item -> !id.equals(item.getId()))
                .forEach(item -> {
                    if (item
                            .getParentid()
                            .equals(id)) {
                        categoryTreeDtoList.add(item);
                    }
                    // 找到当前节点的父节点
                    CourseCategoryTreeDto courseCategoryTreeDto = mapTemp.get(item.getParentid());
                    if (courseCategoryTreeDto != null) {
                        if (courseCategoryTreeDto.getChildrenTreeNodes() == null) {
                            courseCategoryTreeDto.setChildrenTreeNodes(new ArrayList<>());
                        }
                        //下边开始往ChildrenTreeNodes属性中放子节点
                        courseCategoryTreeDto
                                .getChildrenTreeNodes()
                                .add(item);
                    }
                });
        return categoryTreeDtoList;
    }

}

